<?php

class SiteController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
        
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
            
            
            $sqlNews = "SELECT id,title,content,min_banner_url FROM xyh_news WHERE status=1 and is_del=2 ORDER BY id DESC limit 5 ";
            $dataNews = Yii::app()->db->createCommand($sqlNews)->queryAll();
            
       
            //去除符号
            foreach ($dataNews as $k=>$v){
   
                 $dataNews[$k]['content']  =mb_substr(strip_tags($this->trimall($dataNews[$k]['content'])),0,60,'utf-8').'...';
 
            }
            $new =time();
            $sqlActivity = "SELECT a.id,a.title,als.content,a.start_time,a.type_id,als.address,a.min_banner_url,t.name FROM xyh_activity as a"
                         . " LEFT JOIN xyh_activity_link_signup as als on a.id = als.activity_id"
                         . " LEFT JOIN xyh_type as t on a.cat_id = t.id"
                         . " WHERE a.status=1 and a.is_del=2 and a.end_time >{$new} ORDER BY a.id DESC limit 5 ";
            $dataAc = Yii::app()->db->createCommand($sqlActivity)->queryAll();
            
            
            
            foreach ($dataAc as $k=>$v){
                
                 $dataAc[$k]['content']  =mb_substr(strip_tags($this->trimall($dataAc[$k]['content'])),0,86,'utf-8'); 
                 $dataAc[$k]['start_day'] = $this->TimeDifference(time(),$v['start_time']);//计算几天开始 空为已开始
                 $dataAc[$k]['start_time'] = date('Y-m-d H:00',$v['start_time']);
                 
                 if($v['type_id']==102){
                     $sqlQ ="SELECT q_content,sort FROM xyh_activity_link_vote_q WHERE activity_id = {$v['id']} limit 3 ";
                     $dataQ = Yii::app()->db->createCommand($sqlQ)->queryAll();
                     
                     $dataAc[$k]['q_content'] = $dataQ;
                 }
            }
            
            
            
          
			
			   //名人名企
            $sqlRightList ="SELECT n.id,n.title,n.content,min_banner_url FROM xyh_article as n WHERE n.status = 1 and  n.is_hot=2   ORDER by n.id DESC LIMIT 6  ";
            $dataRightList['FC'] = Yii::app()->db->createCommand($sqlRightList)->queryAll();

            //去除符号
            foreach ($dataRightList['FC'] as $k=>$v){
                 $dataRightList['FC'][$k]['content']  =mb_substr(strip_tags($this->trimall($dataRightList['FC'][$k]['content'])),0,30,'utf-8').'...';
            }
            
      
            
             //MBA
            $sqlRightList ="SELECT n.id,n.title FROM xyh_article as n WHERE n.status = 1 and  n.is_hot=2 and type_id=42  ORDER by n.id DESC LIMIT 6  ";
            $dataRightList['MBA']= Yii::app()->db->createCommand($sqlRightList)->queryAll();
            
           
            //排行
          $sqlRightList ="SELECT n.id,n.title,n.zan_count,n.fav_count
                          FROM xyh_news as n
                          WHERE n.status = 1 and  n.is_hot=2  ORDER by n.zan_count DESC LIMIT 6  ";

          $dataRightList['NEW'] = Yii::app()->db->createCommand($sqlRightList)->queryAll();
            
	         //合作院校
           $sqlRightList ="SELECT a.id,city_id,s.name,a.banner_url
                          FROM xyh_alumni as a
                          LEFT JOIN xyh_school as s on s.id = a.school_id
                          WHERE a.status = 1 and a.is_del=2 and a.parent_id<>0  LIMIT 13";

           $dataRightList['school'] = Yii::app()->db->createCommand($sqlRightList)->queryAll();
	   
	   
	   
            
	    $this->render('index',array(
                'News'=>$dataNews,
                'Activity'=>$dataAc,
               'dataRightList'=>$dataRightList
                    ));

	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
		if($error=Yii::app()->errorHandler->error)
		{
			if(Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else
				$this->render('error', $error);
		}
	}

	/**
	 * Displays the contact page
	 */
	public function actionContact()
	{
		$model=new ContactForm;
		if(isset($_POST['ContactForm']))
		{
			$model->attributes=$_POST['ContactForm'];
			if($model->validate())
			{
				$name='=?UTF-8?B?'.base64_encode($model->name).'?=';
				$subject='=?UTF-8?B?'.base64_encode($model->subject).'?=';
				$headers="From: $name <{$model->email}>\r\n".
					"Reply-To: {$model->email}\r\n".
					"MIME-Version: 1.0\r\n".
					"Content-type: text/plain; charset=UTF-8";

				mail(Yii::app()->params['adminEmail'],$subject,$model->body,$headers);
				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
				$this->refresh();
			}
		}
		$this->render('contact',array('model'=>$model));
	}

	/**
	 * Displays the login page
	 */
	public function actionLogin()
	{
		$model=new LoginForm;

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$model->attributes=$_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login())
				$this->redirect(Yii::app()->user->returnUrl);
		}
		// display the login form
		$this->render('login',array('model'=>$model));
	}

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
        
        
        
        
        
        
        function trimall($str)//删除空格
        {
            $qian=array(" "," ","\t","\n","\r","&nbsp;");
            $hou=array("","","","","","");
            return str_replace($qian,$hou,$str);    
        }
        
        
        function  TimeDifference($start,$end){
            
            if(($end -$start)>0){
                
                $day = floor(($end-$start)/86400);
               // $hour=floor((strtotime($enddate)-strtotime($startdate))%86400/3600);
               // $minute=floor((strtotime($enddate)-strtotime($startdate))%86400/60);
               // $second=floor((strtotime($enddate)-strtotime($startdate))%86400%60);
                return $day;
            }else{
                
                return FALSE;
                
            }
        }
        
    
   
}